package cn.iocoder.yudao.module.dr.service.sourceapply;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.SourceApplyPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.SourceApplySaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourceapply.SourceApplyDO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourceapplydetail.SourceApplyDetailDO;
import cn.iocoder.yudao.module.dr.dal.mysql.sourceapply.SourceApplyMapper;
import cn.iocoder.yudao.module.dr.dal.mysql.sourceapplydetail.SourceApplyDetailMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.SOURCE_APPLY_NOT_EXISTS;

/**
 * 资源申请 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SourceApplyServiceImpl implements SourceApplyService {

    @Resource
    private SourceApplyMapper sourceApplyMapper;

    @Resource
    private SourceApplyDetailMapper sourceApplyDetailMapper;

    @Override
    public Long createSourceApply(SourceApplySaveReqVO createReqVO) {
        // 插入
        SourceApplyDO sourceApply = BeanUtils.toBean(createReqVO, SourceApplyDO.class);
        sourceApplyMapper.insert(sourceApply);

        createSourceApplyDetailList(sourceApply.getId(),createReqVO.getSourceApplyDetails());

        // 返回
        return sourceApply.getId();
    }

    @Override
    public void updateSourceApply(SourceApplySaveReqVO updateReqVO) {
        // 校验存在
        validateSourceApplyExists(updateReqVO.getId());
        // 更新
        SourceApplyDO updateObj = BeanUtils.toBean(updateReqVO, SourceApplyDO.class);
        sourceApplyMapper.updateById(updateObj);
        updateSourceApplyDetailList(updateObj.getId(), updateReqVO.getSourceApplyDetails());
    }

    @Override
    public void deleteSourceApply(Long id) {
        // 校验存在
        validateSourceApplyExists(id);
        // 删除
        sourceApplyMapper.deleteById(id);

        deleteSourceApplyDetailList(id);
    }

    private void validateSourceApplyExists(Long id) {
        if (sourceApplyMapper.selectById(id) == null) {
            throw exception(SOURCE_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public SourceApplyDO getSourceApply(Long id) {
        return sourceApplyMapper.selectById(id);
    }

    @Override
    public PageResult<SourceApplyDO> getSourceApplyPage(SourceApplyPageReqVO pageReqVO) {
        return sourceApplyMapper.selectPage(pageReqVO);
    }


    private void createSourceApplyDetailList(Long applyId, List<SourceApplyDetailDO> list) {
        if(CollectionUtils.isEmpty(list)) {
            return;
        }
        if (list != null) {
            list.forEach(o -> o.setApplyId(applyId));
        }
        sourceApplyDetailMapper.insertBatch(list);
    }

    private void updateSourceApplyDetailList(Long applyId, List<SourceApplyDetailDO> list) {
        if(CollectionUtils.isEmpty(list)) {
            return;
        }
        sourceApplyDetailMapper.deleteByApplyId(applyId);
        list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createSourceApplyDetailList(applyId, list);
    }

    private void deleteSourceApplyDetailList(Long applyId) {
        sourceApplyDetailMapper.deleteByApplyId(applyId);
    }

}